package com.ruoyi.project.monitor.job.task;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.service.IDoyumingService;
import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.service.IPersonhostService;
import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import com.ruoyi.project.im.utils.Http;
import com.ruoyi.project.im.utils.RandomNum;
import com.ruoyi.project.system.tongji.domain.UserTongji;
import com.ruoyi.project.system.tongji.service.IUserTongjiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IDoyumingService iDoyumingService;

    @Autowired
    private IPersonhostService iPersonhostService;

    @Autowired
    private IUserTongjiService userTongjiService;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    public void check(){
        Doyuming doyuming=iDoyumingService.selectDoyumingByLastchecktime();//取一条
        if(doyuming==null){
            return;
        }
        String content= HttpUtils.sendGet(Http.CHECKURL+doyuming.getDomain());
        JSONObject object=(JSONObject) JSONObject.parse(content);
        doyuming.setLastCheckTime(new Date());
        if(object.getInteger("status")==1){//正常
            iDoyumingService.updateDoyuming(doyuming);
        }else {
            Doyuming newDoyuming=iDoyumingService.selectDoyumingByZone(doyuming.getLeixing()); //找一条最新的对应类型的域名
            String domain=newDoyuming.getDomain();

            List<Personhost> list=iPersonhostService.selectListByDoyuming(Long.valueOf(doyuming.getLeixing()),doyuming.getDomain());
            for (Personhost personhost : list) {
                personhost.setUrl("http://"+RandomNum.randomxiaoxie(10)+"."+domain);
                iPersonhostService.updatePersonhost(personhost);
            }
            newDoyuming.setFenpeicount(Long.valueOf(list.size()));
            iDoyumingService.updateDoyuming(newDoyuming);
            doyuming.setStatus(3);
            doyuming.setBeizhu(object.getString("info"));
        }
        iDoyumingService.updateDoyuming(doyuming);
//        Users  users= usersService.selectUsersByKamicode("GTSXVAUGVQ-LG");
        System.out.println(object.toString());
    }
    public void everytongji(){
        Date nowTime = new Date();
        List<Users> kfuser=usersService.selectUsersListByEnd();
        for (Users kefuuser: kfuser) {
            Long count=usersService.selectUsersByUserIdcount(kefuuser.getUserId());
            UserTongji userTongji1=new UserTongji();
            UserTongji userTongjitoday=userTongjiService.selectUserTongjiByIdanddate(kefuuser.getUserId());
            if(userTongjitoday==null){
                userTongji1.setUseId(kefuuser.getUserId());
                userTongji1.setUpaccount(kefuuser.getCreateUser());
                userTongji1.setCounts(count);
                userTongji1.setTongjidate(nowTime);
                userTongji1.setUserkami(kefuuser.getKamicode());
                userTongjiService.insertUserTongji(userTongji1);
            }else{
                userTongji1.setCounts(count);
                userTongjiService.updateUserTongji(userTongji1);
            }
        }
        System.out.println("更新统计成功！");
    }
}
